﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Drawing;

public partial class dashboard : System.Web.UI.Page
{
    protected void Page_Prerender(object sender, EventArgs e)
    {
        SqlDataSource2.SelectCommand = "SELECT COUNT(issue_status) AS Count, issue_status AS Status FROM Issue_Master GROUP BY issue_status";
        string default_proj;

        if (Session["selectedproject"].ToString() == "-1")
        {
            default_proj = "0";
        }
        else
        {
            default_proj = Session["selectedproject"].ToString();

            //ddlProjectM.Items.FindByValue(Session["selectedproject"].ToString()).Selected = true;
        }
        string userid = Session["userid"].ToString();
        ClsDBConnection objhdcon = new ClsDBConnection();
        SqlConnection conn = objhdcon.OpenConnection();
        if (default_proj == "0")
        {

            string qryUnassigned = "SELECT issue_id AS [Issue ID], issue_description AS Description FROM Issue_Master WHERE(issue_status = 'unassigned')";
            string qryResolved = "SELECT issue_id AS [Issue ID], issue_description AS Description FROM Issue_Master WHERE(issue_status = 'closed') ORDER BY issue_id ";
            string qryModified = "SELECT issue_id AS [Issue ID], issue_description AS Description, last_modified_date AS [Modified Date] FROM Issue_Master ORDER BY last_modified_date DESC";
            string qryReported = "SELECT issue_id AS [Issue ID], issue_description AS Description FROM Issue_Master WHERE(admin_id='" + userid + "')";

            SqlCommand cmdUnassigned = new SqlCommand(qryUnassigned, conn);
            SqlCommand cmdResolved = new SqlCommand(qryResolved, conn);
            SqlCommand cmdModified = new SqlCommand(qryModified, conn);
            SqlCommand cmdReported = new SqlCommand(qryReported, conn);

            SqlDataReader drUnassigned = cmdUnassigned.ExecuteReader();
            gridUnassigned.DataSource = drUnassigned;
            gridUnassigned.DataBind();
            drUnassigned.Close();

            SqlDataReader drResolved = cmdResolved.ExecuteReader();
            gridResolved.DataSource = drResolved;
            gridResolved.DataBind();
            drResolved.Close();

            SqlDataReader drModified = cmdModified.ExecuteReader();
            gridModified.DataSource = drModified;
            gridModified.DataBind();
            drModified.Close();

            SqlDataReader drReported = cmdReported.ExecuteReader();
            gridReported.DataSource = drReported;
            gridReported.DataBind();
            drReported.Close();


            conn.Close();

        }
        else
        {
            SqlDataSource2.SelectCommand = "SELECT COUNT(issue_status) AS Count, issue_status AS Status FROM Issue_Master WHERE (project_id='" + default_proj + "') GROUP BY issue_status";

            string qryUnassigned = "SELECT issue_id AS [Issue ID], issue_description AS Description FROM Issue_Master WHERE(issue_status = 'unassigned' AND project_id='" + default_proj + "')";
            string qryResolved = "SELECT issue_id AS [Issue ID], issue_description AS Description FROM Issue_Master WHERE(issue_status = 'closed' AND project_id='" + default_proj + "') ORDER BY issue_id ";
            string qryModified = "SELECT issue_id AS [Issue ID], issue_description AS Description, last_modified_date AS [Modified Date] FROM Issue_Master WHERE (project_id='" + default_proj + "') ORDER BY last_modified_date DESC";
            string qryReported = "SELECT issue_id AS [Issue ID], issue_description AS Description FROM Issue_Master WHERE(admin_id='" + userid + "' AND project_id='" + default_proj + "')";

            SqlCommand cmdUnassigned = new SqlCommand(qryUnassigned, conn);
            SqlCommand cmdResolved = new SqlCommand(qryResolved, conn);
            SqlCommand cmdModified = new SqlCommand(qryModified, conn);
            SqlCommand cmdReported = new SqlCommand(qryReported, conn);

            SqlDataReader dr = cmdUnassigned.ExecuteReader();
            gridUnassigned.DataSource = dr;
            gridUnassigned.DataBind();
            dr.Close();

            SqlDataReader drResolved = cmdResolved.ExecuteReader();
            gridResolved.DataSource = drResolved;
            gridResolved.DataBind();
            drResolved.Close();

            SqlDataReader drModified = cmdModified.ExecuteReader();
            gridModified.DataSource = drModified;
            gridModified.DataBind();
            drModified.Close();

            SqlDataReader drReported = cmdReported.ExecuteReader();
            gridReported.DataSource = drReported;
            gridReported.DataBind();
            drReported.Close();


            conn.Close();


        }

    }

    //protected void Page_Load(object sender, EventArgs e)
    //{
    //    SqlDataSource2.SelectCommand = "SELECT COUNT(issue_status) AS Count, issue_status AS Status FROM Issue_Master GROUP BY issue_status";
    //    Label lab = (Label)Master.FindControl("lblName.Text");
    //    //DropDownList ddlProjectM = (DropDownList)Master.FindControl("ddlProject");
    //    ////gridUnassigned.RowDataBound += new GridViewRowEventHandler(gridUnassigned_RowDataBound);

    //    string default_proj;
        
    //    if (Session["selectedproject"].ToString() == "-1")
    //    {
    //        default_proj = "0";
    //    }
    //    else
    //    {
    //        default_proj = Session["selectedproject"].ToString();

    //        //ddlProjectM.Items.FindByValue(Session["selectedproject"].ToString()).Selected = true;
    //    }
    //    string userid = Session["userid"].ToString();
    //    ClsDBConnection objhdcon = new ClsDBConnection();
    //    SqlConnection conn = objhdcon.OpenConnection();
    //    if (default_proj == "0")
    //    {

    //        string qryUnassigned = "SELECT issue_id AS [Issue ID], issue_description AS Description FROM Issue_Master WHERE(issue_status = 'unassigned')";
    //        string qryResolved = "SELECT issue_id AS [Issue ID], issue_description AS Description FROM Issue_Master WHERE(issue_status = 'closed') ORDER BY issue_id ";
    //        string qryModified = "SELECT issue_id AS [Issue ID], issue_description AS Description, last_modified_date AS [Modified Date] FROM Issue_Master ORDER BY last_modified_date DESC";
    //        string qryReported = "SELECT issue_id AS [Issue ID], issue_description AS Description FROM Issue_Master WHERE(admin_id='" + userid + "')";

    //        SqlCommand cmdUnassigned = new SqlCommand(qryUnassigned, conn);
    //        SqlCommand cmdResolved = new SqlCommand(qryResolved, conn);
    //        SqlCommand cmdModified = new SqlCommand(qryModified, conn);
    //        SqlCommand cmdReported = new SqlCommand(qryReported, conn);

    //        SqlDataReader drUnassigned = cmdUnassigned.ExecuteReader();
    //        gridUnassigned.DataSource = drUnassigned;
    //        gridUnassigned.DataBind();
    //        drUnassigned.Close();

    //        SqlDataReader drResolved = cmdResolved.ExecuteReader();
    //        gridResolved.DataSource = drResolved;
    //        gridResolved.DataBind();
    //        drResolved.Close();

    //        SqlDataReader drModified = cmdModified.ExecuteReader();
    //        gridModified.DataSource = drModified;
    //        gridModified.DataBind();
    //        drModified.Close();

    //        SqlDataReader drReported = cmdReported.ExecuteReader();
    //        gridReported.DataSource = drReported;
    //        gridReported.DataBind();
    //        drReported.Close();


    //        conn.Close();

    //    }
    //    else
    //    {
    //        SqlDataSource2.SelectCommand = "SELECT COUNT(issue_status) AS Count, issue_status AS Status FROM Issue_Master WHERE (project_id='" + default_proj + "') GROUP BY issue_status";

    //        string qryUnassigned = "SELECT issue_id AS [Issue ID], issue_description AS Description FROM Issue_Master WHERE(issue_status = 'unassigned' AND project_id='" + default_proj + "')";
    //        string qryResolved = "SELECT issue_id AS [Issue ID], issue_description AS Description FROM Issue_Master WHERE(issue_status = 'closed' AND project_id='" + default_proj + "') ORDER BY issue_id ";
    //        string qryModified = "SELECT issue_id AS [Issue ID], issue_description AS Description, last_modified_date AS [Modified Date] FROM Issue_Master WHERE (project_id='" + default_proj + "') ORDER BY last_modified_date DESC";
    //        string qryReported = "SELECT issue_id AS [Issue ID], issue_description AS Description FROM Issue_Master WHERE(admin_id='" + userid + "' AND project_id='" + default_proj + "')";

    //        SqlCommand cmdUnassigned = new SqlCommand(qryUnassigned, conn);
    //        SqlCommand cmdResolved = new SqlCommand(qryResolved, conn);
    //        SqlCommand cmdModified = new SqlCommand(qryModified, conn);
    //        SqlCommand cmdReported = new SqlCommand(qryReported, conn);

    //        SqlDataReader dr = cmdUnassigned.ExecuteReader();
    //        gridUnassigned.DataSource = dr;
    //        gridUnassigned.DataBind();
    //        dr.Close();

    //        SqlDataReader drResolved = cmdResolved.ExecuteReader();
    //        gridResolved.DataSource = drResolved;
    //        gridResolved.DataBind();
    //        drResolved.Close();

    //        SqlDataReader drModified = cmdModified.ExecuteReader();
    //        gridModified.DataSource = drModified;
    //        gridModified.DataBind();
    //        drModified.Close();

    //        SqlDataReader drReported = cmdReported.ExecuteReader();
    //        gridReported.DataSource = drReported;
    //        gridReported.DataBind();
    //        drReported.Close();


    //        conn.Close();


    //    }


    //}

    public void gridUnassigned_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        ClsDBConnection cn = new ClsDBConnection();
        SqlConnection conn = cn.OpenConnection();
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //if (e.Row.Cells[0].Text == "somevalue") //compare first column of current row value  
            //    e.Row.BackColor = Color.FromName("Red");
            string issue_id = ((HyperLink)e.Row.Cells[0].Controls[0]).Text;
            string qry = "SELECT issue_priority FROM Issue_Master WHERE issue_id='" + issue_id + "'";
            SqlCommand cmd = new SqlCommand(qry, conn);

            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                string prior = dr.GetString(0);
                if (prior == "None")
                {

                    e.Row.BackColor = Color.White;
                }
                else if (prior == "Low")
                {
                    e.Row.BackColor = ColorTranslator.FromHtml("#C2DFFF");
                }

                else if (prior == "Normal")
                {
                    e.Row.BackColor = ColorTranslator.FromHtml("#FFF494");
                }
                else if (prior == "High")
                {
                    e.Row.BackColor = ColorTranslator.FromHtml("#FFCD85");
                }
                else if (prior == "Urgent")
                {
                    e.Row.BackColor = ColorTranslator.FromHtml("#FCBDBD");
                }
                else
                {
                    e.Row.BackColor = ColorTranslator.FromHtml("#C9CCC4"); ;
                }
            }
            dr.Close();
        } conn.Close();
    }

    
}